package exttable.threads;

import dbutils.DbUtils;
import demo.cypher.FileEncrypter;
import exttable.formgenerator.StaticMethods;
import exttable.questionnaire.QuestionnaireAnalyser;
import exttable.timetable.xlsgenerator.StatReportAnalysis;
import exttable.view.ViewTestComboBox;
import options.AppSettings;
import options.SettingsFrame;
import utils.Localisation;

import javax.swing.*;
import java.io.*;

/**
 * Created by IntelliJ IDEA.
 * User: admin
 * Date: 16.04.2011
 * Time: 1:37:08
 * To change this template use File | Settings | File Templates.
 */
public class ThreadFactory {
    public static Runnable runMysqlDump(final File file) {
        return new Runnable() {
            @Override
            public void run() {
                final String commandDump = "cmd /c " + "mysqldump -h" + DbUtils.getIpAddress() + " -u root -p" + DbUtils.getPass() + " "+DbUtils.GENERAL_DATABASE_NAME +" > " + " \"" + file.getAbsolutePath() + "\"";
//                final String commandZip = "C:/Program Files/WinRAR/WinRAR a -dw -ep " + " \"" + dirPath + fileName + ".zip \"" + " \"" + dirPath + fileName + "\"" + " -hpjavasun";
//                final String command7Zip = "C:/Program Files/7-Zip/7z a  " + " \"" + dirPath + fileName + ".zip \"" + " \"" + dirPath + fileName + "\"" + " -pjavasun";
//                final String commandDelete = "cmd /c " + dirPath.replace('/', '\\') + " del " + fileName;
//                System.out.println("commandDump " + commandDump);
//                System.out.println("commandZip " + commandZip);
//                System.out.println("command7Zip " + command7Zip);
                StaticMethods.getProgressFrame("progressFrame creating zip", Localisation.getMessage("creatingFile"));

                try {
                    Runtime.getRuntime().exec(commandDump).waitFor();
                    FileEncrypter ff=new FileEncrypter("javasun");
                    ff.encrypt(new FileInputStream(file), new FileOutputStream(file.getAbsoluteFile() + "_en") );
                    File fileEn=new File(file.getAbsolutePath()+"_en");
                    StaticMethods.createZip(file.getAbsolutePath() + "_en.zip", fileEn);
                    file.delete();
                    fileEn.delete();
                    StaticMethods.getFrameByName("progressFrame creating zip").dispose();
                    JOptionPane.showMessageDialog(null, Localisation.getMessage("info.filecreated") + file.getAbsolutePath() + "_en.zip",
                    Localisation.getMessage("info.title"), JOptionPane.INFORMATION_MESSAGE);
//                    Thread runEnc = new Thread(ThreadFactory.runEncrypt(file, new FileOutputStream(file.getAbsoluteFile() + "_en")));
//                    runEnc.start();
//                    StaticMethods.getFrameByName("progressFrame creating zip").dispose();
                } catch (InterruptedException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                } catch (IOException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }
        };
    }

    public static Runnable runEncrypt(final File file, final OutputStream output) {
        return new Runnable() {
            @Override
            public void run() {
                FileEncrypter ff = new FileEncrypter("javasun");

                try {
                    ff.encrypt(new FileInputStream(file), output);
//                    System.out.println("file info:" + file.getAbsolutePath() + " " + file.getName());
                    StaticMethods.getFrameByName("progressFrame creating zip").dispose();
                    File fileEn=new File(file.getAbsolutePath()+"_en");
                    StaticMethods.createZip(file.getAbsolutePath() + "_en.zip", fileEn);
                    file.delete();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }

            }
        };
    }

    public static Runnable runStatReport(final ViewTestComboBox vcb, final String path, final String phaseID, final boolean newVersion, final boolean translateColumnName) {
        return new Runnable() {

            @Override
            public void run() {
                try {
                    StaticMethods.getProgressFrame("creatingExcellForAnalysys", Localisation.getMessage("creatingFile"));
                    StatReportAnalysis.formXLSReport(vcb, path, phaseID, newVersion, translateColumnName);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        };
    }

    public static Runnable runQuestionnaireAnalyser(final String filePath) {
        return new Runnable() {

            @Override
            public void run() {
                try {
                    StaticMethods.getProgressFrame("RunQuestionnaireAnalyser", Localisation.getMessage("creatingFile"));
                    new QuestionnaireAnalyser(filePath);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        };
    }

    public static Runnable runSaveConfig(){
        return new Runnable() {

            @Override
            public void run() {
                StaticMethods.getProgressFrame("progressFrame saveConfig", Localisation.getMessage("processRunning"));
                try {
                    AppSettings.saveConfig();
                    JOptionPane.showMessageDialog(null, Localisation.getMessage("info.dataadded"), "", JOptionPane.INFORMATION_MESSAGE);
                    StaticMethods.getFrameByName("settingsFrame").dispose();
                    if(!SettingsFrame.isLogIn()){
                        JOptionPane.showMessageDialog(null, "to save changes reload programm");
                        Runtime.getRuntime().exec("java -jar "+ThreadFactory.class.getProtectionDomain().getCodeSource().getLocation().getPath()+"staff.jar");
//						Runtime.getRuntime().exec("start staff.bat");
                        System.exit(0);
                    }
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, Localisation.getMessage("error.applicationerror"), "error",
                            JOptionPane.ERROR_MESSAGE);
                    e.printStackTrace();
                }
                finally{
                    StaticMethods.getFrameByName("progressFrame saveConfig").dispose();
                }
            }
        };

    }
}
